package controller

import (
	"doctor_api_backend/config"
	"doctor_api_backend/models"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 获取医生病人映射表
func GetDoctorPatientMapping(c *gin.Context) {

}

// 获取所有医生病人映射表
func GetAllDoctorPatientMapping(c *gin.Context) {

	var doctorPatientMappings []models.DoctorPatientMapping
	if err := config.DB.Find(&doctorPatientMappings).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询医生病人映射表失败"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMappings)

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMappings)
}

// 添加医生病人映射表
func AddDoctorPatientMapping(c *gin.Context) {
	var doctorPatientMapping models.DoctorPatientMapping
	if err := c.ShouldBindJSON(&doctorPatientMapping); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	config.DB.Create(&doctorPatientMapping)
	c.JSON(http.StatusCreated, doctorPatientMapping)

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMapping)
}

// 更新医生病人映射表
func UpdateDoctorPatientMapping(c *gin.Context) {
	id := c.Param("id")
	var doctorPatientMapping models.DoctorPatientMapping
	if err := config.DB.First(&doctorPatientMapping, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "医生病人映射表不存在"})
		return
	}

	if err := c.ShouldBindJSON(&doctorPatientMapping); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	config.DB.Save(&doctorPatientMapping)
	c.JSON(http.StatusOK, doctorPatientMapping)
}

// 删除医生病人映射表, 找到doctor_patient_mapping_code才删除成功，否者打印删除失败，不存在
func DeleteDoctorPatientMapping(c *gin.Context) {
	doctorPatientMappingCode := c.Param("doctorPatientMappingCode")
	var doctorPatientMapping models.DoctorPatientMapping
	if err := config.DB.Where("doctor_patient_mapping_code = ?", doctorPatientMappingCode).First(&doctorPatientMapping).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "医生病人映射表不存在"})
		return
	}

	config.DB.Delete(&doctorPatientMapping)

	config.DB.Delete(&doctorPatientMapping)
	c.JSON(http.StatusOK, gin.H{"message": "医生病人映射表删除成功"})

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMapping)
}

// 查询医生病人映射表
func GetDoctorPatientMappingByDoctorId(c *gin.Context) {
	doctorId := c.Param("doctorId")

	var doctorPatientMappings []models.DoctorPatientMapping
	if err := config.DB.Where("doctor_id = ?", doctorId).Find(&doctorPatientMappings).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询医生病人映射表失败"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMappings)

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMappings)
}

// 查询医生病人映射表
func GetDoctorPatientMappingByPatientId(c *gin.Context) {
	patientId := c.Param("patientId")

	var doctorPatientMappings []models.DoctorPatientMapping
	if err := config.DB.Where("patient_id = ?", patientId).Find(&doctorPatientMappings).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询医生病人映射表失败"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMappings)
}

// 获取医生病人映射表 by id
func GetDoctorPatientMappingByID(c *gin.Context) {
	id := c.Param("id")
	var doctorPatientMapping models.DoctorPatientMapping
	if err := config.DB.First(&doctorPatientMapping, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "医生病人映射表不存在"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMapping)
}

// 创建医生病人映射表
func CreateDoctorPatientMapping(c *gin.Context) {
	var doctorPatientMapping models.DoctorPatientMapping
	if err := c.ShouldBindJSON(&doctorPatientMapping); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	config.DB.Create(&doctorPatientMapping)
	c.JSON(http.StatusCreated, doctorPatientMapping)
}

// 获取医生病人映射表 by doctor id
func GetDoctorPatientMappingByDoctorID(c *gin.Context) {
	doctorId := c.Param("doctorId")

	var doctorPatientMappings []models.DoctorPatientMapping
	if err := config.DB.Where("doctor_id = ?", doctorId).Find(&doctorPatientMappings).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询医生病人映射表失败"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMappings)

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMappings)
}

// 获取医生病人映射表 by patient id
func GetDoctorPatientMappingByPatientID(c *gin.Context) {
	patientId := c.Param("patientId")

	var doctorPatientMappings []models.DoctorPatientMapping
	if err := config.DB.Where("patient_id = ?", patientId).Find(&doctorPatientMappings).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询医生病人映射表失败"})
		return
	}
	c.JSON(http.StatusOK, doctorPatientMappings)

	// 打印医生病人映射表
	fmt.Println("医生病人映射表", doctorPatientMappings)
}
